package com.cq.xjms.service.impl;

import java.util.List;

import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.extremecomponents.table.limit.Limit;

import com.cq.xjms.model.Course;
import com.cq.xjms.model.Student;
import com.cq.xjms.service.StudentManager;

public class StudentManagerImpl extends BaseManagerImpl<Student> implements StudentManager{
	private static Logger LOG = Logger.getLogger(StudentManagerImpl.class);
	public List<Student> queryStudent(String name,String stuNo,String idcard,Long clazzId,Limit limit) {
		StringBuffer hql = new StringBuffer();
		hql.append("select c from Student c left join fetch c.clazz t where 1=1 ");
		if(StringUtils.isNotEmpty(name)){
			hql.append(" and c.name=:name");
		}
		if(StringUtils.isNotEmpty(stuNo)){
			hql.append(" and c.stuNo=:stuNo");
		}
		if(clazzId!=null){
			hql.append(" and c.clazz.id=:clazzId");
		}
		if(StringUtils.isNotEmpty(idcard)){
			hql.append(" and c.idcard=:idcard");
		}
		hql.append(" order by c.createDate desc");
		List<Student> students = null;
		try{
			Query query = this.query(hql.toString());
			if(StringUtils.isNotEmpty(name)){
				query.setParameter("name", name);
			}
			if(StringUtils.isNotEmpty(stuNo)){
				query.setParameter("stuNo", stuNo);
			}
			if(clazzId!=null){
				query.setParameter("clazzId", clazzId);
			}
			if(StringUtils.isNotEmpty(idcard)){
				query.setParameter("idcard", idcard);
			}
			if (limit != null) {
				query.setFirstResult((limit.getPage() - 1)
						* limit.getCurrentRowsDisplayed());
				query.setMaxResults(limit.getCurrentRowsDisplayed());
			}
			students =  query.getResultList();
		}catch(Exception e){
			LOG.error("query student failed!", e);
		}
		return students;
	}
	
	
	public int countStudent(String name,String stuNo,String idcard,Long clazzId) {
		StringBuffer hql = new StringBuffer();
		hql.append("select count(c) from Student c  where 1=1 ");
		if(StringUtils.isNotEmpty(name)){
			hql.append(" and c.name=:name");
		}
		if(StringUtils.isNotEmpty(stuNo)){
			hql.append(" and c.stuNo=:stuNo");
		}
		if(clazzId!=null){
			hql.append(" and c.clazz.id=:clazzId");
		}
		if(StringUtils.isNotEmpty(idcard)){
			hql.append(" and c.idcard=:idcard");
		}
		try{
			Query query = this.query(hql.toString());
			if(StringUtils.isNotEmpty(name)){
				query.setParameter("name", name);
			}
			if(StringUtils.isNotEmpty(stuNo)){
				query.setParameter("stuNo", stuNo);
			}
			if(clazzId!=null){
				query.setParameter("clazzId", clazzId);
			}
			if(StringUtils.isNotEmpty(idcard)){
				query.setParameter("idcard", idcard);
			}
			
			Object result =  query.getSingleResult();
			return result == null ? 0
					: result instanceof Integer ? ((Integer) result).intValue()
							: ((Long) result).intValue();
		}catch(Exception e){
			LOG.error("count student failed!", e);
		}
		return 0;
	}
	
}
